#ifndef PttyConnection_First
#define PttyConnection_First
#ifdef __GNUG__
#pragma interface
#endif

#include "Object.h"
#include "Point.h"

class PttyImpl;

#define PTTY_ECHO 1
#define PTTY_RAWORCBREAK 2

//---- PttyChars ---------------------------------------------------------------
//     struct to return the settings of the various tty characters

struct PttyChars {
	char erase;
	char kill;
	char rprnt;
	char susp;
	char intr;
	char quit;
	char eof;
};

//---- ProcState ---------------------------------------------------------------

enum ProcState {
	eProcInited,
	eProcRunning,
	eProcStopped,
	eProcKilled,    // code contains signal number
	eProcExited,    // code contains return code
	eProcTerminated
};
	
//---- PttyConnection ----------------------------------------------------------
//     class for a connection with a pseudo tty

class PttyConnection : public Object {
public:
	MetaDef(PttyConnection);
	PttyConnection();
	~PttyConnection();
	
	bool Run(const char *name, const char **argv);
	void Kill();

	bool IsEcho();
	bool IsRawOrCBreak();
	bool IsRunning();

	int CCErase();
	int CCKill();
	int CCReprint();
	int CCSuspend();
	int CCIntr();
	int CCQuit();
	int CCEof();

	int Write(const char *buf, int n);
	int Read(char *buf, int n);
	void BecomeConsole();
	void SetSize(const Point &cr);

	// up calls
	virtual void DiedNotify();
	virtual void InputNotify();
	virtual void ErrorNotify(int code, const char *msg);

protected:
	int mode;
	PttyImpl *pttycon;
	ProcState state;
};

//---- PttyImpl ----------------------------------------------------------------

class PttyImpl : public Object {
public:
	MetaDef(PttyImpl);
	PttyImpl();
	virtual ~PttyImpl();
	void SetPty(PttyConnection*);
	int GetFileNo();
	int GetPid();
	void ErrorNotify(int code, const char *msg);
	void DiedNotify();

	virtual int SpawnSlave(const char *name, const char **args);
	virtual int KillChild();
	virtual int Flush();
	virtual int GetMode();
	virtual int BecomeConsole();
	virtual int SetWinSize(const Point &cr);
	
public:
	PttyChars pttychars;    // tty-chararacters that submit a command
protected:
	PttyConnection *pty;
	int slavepid, masterfd;
};

//---- inlines -----------------------------------------------------------------

inline void PttyImpl::SetPty(PttyConnection *p)
	{ pty= p; }
inline int PttyImpl::GetFileNo()
	{ return masterfd; }
inline int PttyImpl::GetPid()
	{ return slavepid; }

inline bool PttyConnection::IsEcho()
	{ return mode & PTTY_ECHO; }
inline bool PttyConnection::IsRawOrCBreak()
	{ return mode & PTTY_RAWORCBREAK; }

inline int PttyConnection::CCErase()
	{ return pttycon->pttychars.erase; }
inline int PttyConnection::CCKill()
	{ return pttycon->pttychars.kill; }
inline int PttyConnection::CCReprint()
	{ return pttycon->pttychars.rprnt; }
inline int PttyConnection::CCSuspend()
	{ return pttycon->pttychars.susp; }
inline int PttyConnection::CCIntr()
	{ return pttycon->pttychars.intr; }
inline int PttyConnection::CCQuit()
	{ return pttycon->pttychars.quit; }
inline int PttyConnection::CCEof()
	{ return pttycon->pttychars.eof; }
	
#endif
